using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.IO;
using Entities;
using Bll;

public partial class Admin_EditProductInDetail : System.Web.UI.Page
{
    ProductBll proBll;
    ProductInfo proInfo;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetText(TestQuery());
        }
    }

    private void ClearText()
    {
        txtCategoryID.Text = "";
        txtProductName.Text = "";
        txtUnitsPrice.Text = "";
        txtUnitsInStock.Text = "";
        txtUnitsOnOrder.Text = "";
        ftbFeatures.Text = "";
        ftbDescriptionBrief.Text = "";
        ftbDescription.Text = "";
        previewImage.ImageUrl = @"../CoreLayout/ProductImages/no-photo.jgp";
    }

    private void GetText(string productID)
    {        
        proBll = new ProductBll();
        ProductInfo proInfo = proBll.GetByID(productID);
        txtProductID.Text = proInfo.ProductID.ToString();
        txtCategoryID.Text = proInfo.CategoryID.ToString();
        txtProductName.Text = proInfo.ProductName;
        txtUnitsPrice.Text = proInfo.UnitPrice.ToString();
        txtUnitsInStock.Text = proInfo.UnitsInStock.ToString();
        txtUnitsOnOrder.Text = proInfo.UnitsOnOrder.ToString();
        ftbFeatures.Text = proInfo.Features;
        ftbDescriptionBrief.Text = proInfo.DescriptionBrief;
        ftbDescription.Text = proInfo.Description;
        previewImage.ImageUrl = proInfo.Picture;
    }

    private string TestQuery()
    {
        if (Request.QueryString.Count > 0)
        {
            return Request.QueryString[0].ToString();
        }
        else
        {
            return "1";
        }
    }

    protected void rbSelectCommand_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rbSelectCommand.SelectedValue == "Edit")
        {
            GetText(TestQuery());
            btnEdit.Visible = true;
            btnEdit.Text = "Update";
            btnCancel.Visible = true;
            btnInsert.Visible = false;
            btnDelete.Visible = false;
        }
        if (rbSelectCommand.SelectedValue == "Insert")
        {
            ClearText();
            btnCancel.Visible = true;
            btnInsert.Visible = true;
            btnEdit.Visible = false;
            btnDelete.Visible = false;
        }
        if (rbSelectCommand.SelectedValue == "Delete")
        {
            GetText(TestQuery());
            btnDelete.Visible = true;
            btnEdit.Visible = false;
            btnInsert.Visible = false;
            btnCancel.Visible = false;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        HttpPostedFile myPostedFile = myFile.PostedFile;

        if (myPostedFile != null)
        {
            string fileUploadName = myPostedFile.FileName;
            if (!string.IsNullOrEmpty(fileUploadName))
            {
                try
                {
                    int size = int.Parse(txtSize.Text);
                    SaveImage(txtProductID.Text, myPostedFile, fileUploadName, size);
                    previewImage.Visible = true;
                    previewImage.ImageUrl = @"../CoreLayout/ProductImages/" + fileUploadName;
                    UpdateMethod(previewImage.ImageUrl);
                }
                catch (System.FormatException fe)
                {
                    if (fe.Message != null)
                    {
                        previewImage.Visible = true;
                        SaveImage(txtProductID.Text, myPostedFile, fileUploadName, 300);
                        previewImage.ImageUrl = @"../CoreLayout/ProductImages/" + fileUploadName;
                        UpdateMethod(previewImage.ImageUrl);
                    }
                }
            }
            else
            {
                lblValidate.Text = "You have to choose a picture file !";
            }
        }
        else
        {
            lblValidate.Text = "No file !";
        }

    }

    private void UpdateMethod(string imageUrl)
    {
        proBll = new ProductBll();
        proInfo = new ProductInfo();
        proInfo.ProductID = int.Parse(txtProductID.Text);
        proInfo.CategoryID = int.Parse(txtCategoryID.Text);
        proInfo.ProductName = txtProductName.Text;
        proInfo.UnitPrice = decimal.Parse(txtUnitsPrice.Text);
        proInfo.UnitsInStock = int.Parse(txtUnitsInStock.Text);
        proInfo.UnitsOnOrder = int.Parse(txtUnitsOnOrder.Text);
        proInfo.Features = ftbFeatures.Text;
        proInfo.DescriptionBrief = ftbDescriptionBrief.Text;
        proInfo.Description = ftbDescription.Text;
        proInfo.Picture = imageUrl;
        bool result = proBll.Update(proInfo);
        if (result == true)
            Response.Redirect("EditProduct.aspx");
        else
            lblError.Text = "Update fail ! please try again";

    }

    private void InsertMethod(string imageUrl)
    {
        proBll = new ProductBll();
        proInfo = new ProductInfo();
        //proInfo.ProductID = int.Parse(txtProductID.Text);
        proInfo.CategoryID = int.Parse(txtCategoryID.Text);
        proInfo.ProductName = txtProductName.Text;
        proInfo.UnitPrice = decimal.Parse(txtUnitsPrice.Text);
        proInfo.UnitsInStock = int.Parse(txtUnitsInStock.Text);
        //proInfo.UnitsOnOrder = int.Parse(txtUnitsOnOrder.Text);
        proInfo.Features = ftbFeatures.Text;
        proInfo.DescriptionBrief = ftbDescriptionBrief.Text;
        proInfo.Description = ftbDescription.Text;
        proInfo.Picture = imageUrl;
        bool result = proBll.Insert(proInfo);
        if (result == true)
            Response.Redirect("EditProduct.aspx");
        else
            lblError.Text = "Insert fail ! please try again";
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        HttpPostedFile myPostedFile = myFile.PostedFile;

        if (myPostedFile != null)
        {
            string fileUploadName = myPostedFile.FileName;
            if (!string.IsNullOrEmpty(fileUploadName))
            {
                try
                {
                    int size = int.Parse(txtSize.Text);
                    SaveImage(txtProductID.Text, myPostedFile, fileUploadName, size);
                    previewImage.Visible = true;
                    previewImage.ImageUrl = @"../CoreLayout/ProductImages/" + fileUploadName;
                    InsertMethod(previewImage.ImageUrl);
                }
                catch (System.FormatException fe)
                {
                    if (fe.Message != null)
                    {
                        previewImage.Visible = true;
                        SaveImage(txtProductID.Text, myPostedFile, fileUploadName, 300);
                        previewImage.ImageUrl = @"../CoreLayout/ProductImages/" + fileUploadName;
                        InsertMethod(previewImage.ImageUrl);
                    }
                }
            }
            else
            {
                lblValidate.Text = "You have to choose a picture file !";
            }
        }
        else
        {
            lblValidate.Text = "No file !";
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        proBll = new ProductBll();
        proInfo = new ProductInfo();
        proInfo.ProductID = int.Parse(txtProductID.Text);
        bool result = proBll.Delete(proInfo);
        if (result == true)
            Response.Redirect("EditProduct.aspx");
        else
            lblError.Text = "Delete fail ! please try again";
    }

    private void SaveImage(string productID, HttpPostedFile postedFile, string clientFileName, int imageSize)
    {
        string savedFolder = @"CoreLayout/ProductImages/";
        string realSaveFolder = Server.MapPath(savedFolder);
        string serverPath = realSaveFolder.Remove(17, 6);
        string uploadedFileName = serverPath + clientFileName;
        string extention = Path.GetExtension(uploadedFileName);

        if (extention == ".jpg" || extention == ".bmp" || extention == ".gif" || extention == ".png")
        {
            Bll.ProductBll proBll = new Bll.ProductBll();
            proBll.SaveUploadImage(postedFile, uploadedFileName, imageSize);

            string path = @"~/CoreLayout/ProductImages/";
            bool result = proBll.UpdatePicture(path + clientFileName, productID);
            if (result)
                lblValidate.Text = "File has been sent !";
            else
                lblValidate.Text = "Can not send file !";
        }
        else
        {
            lblValidate.Text = "Only jpg/bmp/gif/png file extention is allowed";
        }
    }
}
